package com.duoketech.utils;

import java.io.*;
import java.util.List;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Template;
import com.jfinal.template.source.ClassPathSourceFactory;

import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;


/**
 * 文件渲染工具类
 *
 */
public class EngineUtil {


    private static final String pageOutDir = "/src/main/webapp/WEB-INF/view/front/travel/";
    public static String modelPackage ="com.duoketech.model";
    public static String servicePackage="com.duoketech.service";
    public static String controllerPackage="com.duoketech.controller";

    public static Engine initEngine(){
        Engine engine = Engine.create("codeMachine");
        engine.setBaseTemplatePath(null);
        engine.setSourceFactory(new ClassPathSourceFactory());
        engine.setDevMode(true);
        engine.addSharedMethod(new PageUtil());
        engine.addSharedFunction("template/common/macro.html");
        return engine;
    }

    public static String  readFileContentFromResource(Engine engine,Kv kv,String fileName) {

        Template template = engine.getTemplate(fileName);
        String content = template.renderToString(kv);
        return content;
    }

    /*
        创建文件
     */
    public static void createFile(String outPutBasePath,String fileName,String resultContent) {
        try {
            File file = new File(outPutBasePath+File.separator+File.separator);
            if(!file.exists()){
                file.mkdirs();
            }
            FileOutputStream o=null;
            o = new FileOutputStream(file+File.separator+fileName);
            o.write(resultContent.getBytes("UTF-8"));
            o.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void execute(Engine engine,String tableName,String modelName,String packge){

        createModel(engine,tableName,modelName,packge);
        createService(engine,tableName,modelName,packge);
        createController(engine,tableName,modelName,packge);

        createFormView(engine,tableName,modelName,packge);
        createAddOrEditView(engine,tableName,modelName,packge);
        createListView(engine,tableName,modelName,packge);
    }


    public static void createModel(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/backend/model.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("modelName",modelName);
        kv.set("package",packge);
        kv.set("modelPackage",modelPackage);
        kv.set("tableName",tableName);
        String content = readFileContentFromResource(engine,kv,fileName);
        String result=modelPackage.replace(".","/");
        String basePath = PathKit.getWebRootPath()+"/src/main/java/"+result+"/"+packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,modelName+".java",content);
    }


    public static void createService(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/backend/service.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("modelName",modelName);
        kv.set("package",packge);
        kv.set("servicePackage",servicePackage);
        kv.set("tableName",tableName);
        String content = readFileContentFromResource(engine,kv,fileName);
        String result=servicePackage.replace(".","/");
        String basePath = PathKit.getWebRootPath()+"/src/main/java/"+result+"/"+packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,modelName+"Service.java",content);
    }

    public static void createController(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/backend/controller.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("modelName",modelName);
        kv.set("package",packge);
        kv.set("controllerPackage",controllerPackage);
        kv.set("tableName",tableName);
        kv.set("instanceName",StrKit.firstCharToLowerCase(modelName));
        String content = readFileContentFromResource(engine,kv,fileName);
        String result=controllerPackage.replace(".","/");
        String basePath = PathKit.getWebRootPath()+"/src/main/java/"+result+"/"+packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,modelName+"Controller.java",content);
    }


    public static void createListView(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/front/list.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("instanceName",StrKit.firstCharToLowerCase(modelName));
        String content = readFileContentFromResource(engine,kv,fileName);
        String basePath = PathKit.getWebRootPath()+ pageOutDir +packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,StrKit.firstCharToLowerCase(modelName)+"-list.html",content);
    }

    public static void createFormView(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/front/form.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("instanceName",StrKit.firstCharToLowerCase(modelName));
        String content = readFileContentFromResource(engine,kv,fileName);
        String basePath = PathKit.getWebRootPath()+ pageOutDir +packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,StrKit.firstCharToLowerCase(modelName)+"-form.html",content);
    }

    public static void createAddOrEditView(Engine engine,String tableName,String modelName,String packge){
        String fileName = "/template/common/front/add.html";
        List<Record> records = DbUtil.queryColumnBy(tableName);
        Kv kv = Kv.by("columns",records);
        kv.set("instanceName",StrKit.firstCharToLowerCase(modelName));
        String content = readFileContentFromResource(engine,kv,fileName);
        String basePath = PathKit.getWebRootPath()+ pageOutDir +packge;
        basePath = basePath.replace("/",File.separator);
        createFile(basePath,"add-"+StrKit.firstCharToLowerCase(modelName)+".html",content);
        createFile(basePath,"edit-"+StrKit.firstCharToLowerCase(modelName)+".html",content);

    }

    public static void main(String[] args) {
        DbUtil.initSchemaInfoDb();
        Engine engine = initEngine();
        EngineUtil.execute(engine,"t_delivery_config","DeliveryConfig","deliveryconfig");
//        EngineUtil.execute(engine,"t_food_distribute","FoodDistribute","distribute");
//        EngineUtil.execute(engine,"t_activity","Activity","activity");
//        EngineUtil.execute(engine,"t_unit_room","UnitRoom","room");
//        EngineUtil.execute(engine,"t_member","Member","member");
//        EngineUtil.execute(engine,"t_order","Order","order");
    }
}
